﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContractManager = BotonJXC.Entity.ContractManager;
using BotonJXC.Util;
using NHibernate;
using NHibernate.Criterion;
using BotonJXC.Entity;

namespace BotonJXC.DAO
{
    public class ContractManagerDAO : NHibernateDao<ContractManager, int>
    {
        public void saveContractManager(ContractManager contractManager)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Save(contractManager);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;
               
            }
            finally {
                HBHelper.Close();
            }
           
        }

        public void updateContractManager(ContractManager contractManager)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Update( contractManager );
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                HBHelper.Close();
            }

        }
        public void delContractManager( int contractId) {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Delete( contractId );
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                HBHelper.Close();
            }
        }

        public PageModel<ContractManager> findAllContractManager(DateTime begin_time, DateTime end_time, ContractManager contractManagert, int start, int count)
        {

            List<ICriterion> criterions = new List<ICriterion>();
            if (begin_time != null)
            {
                criterions.Add(Restrictions.Ge("ContractDate", begin_time));
            }
            if (end_time != null)
            {
                criterions.Add(Restrictions.Le("ContractDate", end_time));
            }
            if (contractManagert != null)
            {
                if (!string.IsNullOrEmpty(contractManagert.ContractName))
                {
                    criterions.Add(Restrictions.Like("ContractName", contractManagert.ContractName, MatchMode.Anywhere));
                }
                if (!string.IsNullOrEmpty(contractManagert.FirstParty))
                {
                    criterions.Add(Restrictions.Like("FirstParty", contractManagert.FirstParty, MatchMode.Anywhere));
                }
                if (!string.IsNullOrEmpty(contractManagert.SecondParty))
                {
                    criterions.Add(Restrictions.Like("SecondParty", contractManagert.SecondParty, MatchMode.Anywhere));
                }
                if (!string.IsNullOrEmpty(contractManagert.ContractAmount))
                {
                    criterions.Add(Restrictions.Like("ContractAmount", contractManagert.ContractAmount, MatchMode.Anywhere));
                }
            }

            Order order = new Order("CreateTime", false);
            return base.FindByPage(criterions.ToArray(), order, start, count);
        }

      
    }
}
